package com.example.zyf.sortlistviewandsidebar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;


public class SideBar extends View {
    // 点击字母栏字符变化监听器
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;

    // 26
    public static String[] b = {
            PinyinComparator.SELECTED_CITY.substring(0, 2), "A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int choose = -1;// ѡ��
    private Paint paint = new Paint();

    private TextView mTextDialog;

    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置字母栏填充字符
     *
     * @param b
     */
    public void setCityInitials(String[] b) {
        SideBar.b = b;
        invalidate();
    }

    public SideBar(Context context) {
        super(context);
    }

    /**
     * 绘制
     */
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();// view 高度
        int width = getWidth(); // view 宽度
        int singleHeight = height / b.length;// 每行数据高度

        for (int i = 0; i < b.length; i++) {
            paint.setColor(Color.rgb(0, 136, 199));
            // paint.setColor(Color.WHITE);
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(getTextSize());
            // 选中的颜色
            if (i == choose) {
                paint.setColor(Color.parseColor("#99FF00"));
                paint.setFakeBoldText(true);
            }else {
                paint.setColor(Color.parseColor("#3399ff"));
                paint.setFakeBoldText(false);
            }
            //绘制text的坐标
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();// 画笔重置
        }

    }

    public int getMobileDPI(Context context) {
        return context.getResources().getDisplayMetrics().densityDpi;

    }

    private float getTextSize() {
        int dpi = getMobileDPI(getContext());
        if (dpi >= getContext().getResources().getDisplayMetrics().DENSITY_XXXHIGH) {
            return 30;
        } else if (dpi >= getContext().getResources().getDisplayMetrics().DENSITY_XXHIGH) {
            return 25;
        } else if (dpi >= getContext().getResources().getDisplayMetrics().DENSITY_XHIGH) {
            return 20;
        } else if (dpi >= getContext().getResources().getDisplayMetrics().DENSITY_HIGH) {
            return 16;
        } else {
            return 12;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 相对于控件的y坐标
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / getHeight() * b.length);// c为当前选中字符的标示

        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00ffffff")));
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.GONE);
                }
                break;

            default:
                setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80D9D6D5")));
                if (oldChoose != c) {
                    if (c >= 0 && c < b.length) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(b[c]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }

                        choose = c;
                        invalidate();
                    }
                }

                break;
        }
        return true;
    }

    /**
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    /**
     * @author coder
     */
    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(String s);
    }

}